{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "22b56dbb-8f28-4411-818f-de573f42dfb6",
   "metadata": {},
   "source": [
    "箱型图提供了识别异常值的一个标准: 异常值通常被定义为小于 𝑄𝐿 −1.5𝐼𝑄𝑅 或大于 𝑄𝑈 +1.5𝐼𝑄𝑅 的值。𝑄𝐿\r\n",
    "称为下四分位数, 表示全部观察值中有四分之一的数据取值比它小; 𝑄𝑈 称为上四分位数, 表示全部观察值中有四\r\n",
    "分之一的数据取值比它大; 𝐼𝑄𝑅 称为四分位数间距, 是上四分位数 𝑄𝑈 与下四分位数 𝑄𝐿 之差, 其间包含了全部观\r\n",
    "察值的一半。\r\n",
    "箱型图依据实际数据绘制, 没有对数据作任何限制性要求 (如服从某种特定的分布形式), 它只是真实直观地\r\n",
    "表现数据分布的本来面貌; 另一方面, 箱型图判断异常值的标准以四分位数和四分位距为基础, 四分位数具有一\r\n",
    "定的鲁棒性: 多达 25% 的数据可以变得任意远而不会很大地扰动四分位数, 所以异常值不能对这个标准施加影\r\n",
    "响。由此可见, 箱型图识别异常值的结果比较客观, 在识别异常值方面有一定的优越性,\r\n",
    "四分位距 (IQR) 就是上四分位与下四分位的差值。而我们通过 IQR 的 1.5 倍为标准, 规定: 超过 (上四分位\r\n",
    "+1.5 倍 IQR 距离, 或者下四分位-1.5 倍 IQR 距离) 的点为异常值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99365c25-8097-4365-afeb-44b413840e7e",
   "metadata": {},
   "source": [
    "<function matplotlib.pyplot.boxplot(x, notch=None, sym=None, vert=None, whis=None,\r\n",
    "positions=None, widths=None, patch_artist=None, bootstrap=None,\r\n",
    "usermedians=None, conf_intervals=None, meanline=None, showmeans=None,\r\n",
    "showcaps=None, showbox=None, showfliers=None, boxprops=None, labels=None,\r\n",
    "flierprops=None, medianprops=None, meanprops=None, capprops=None,\r\n",
    "whiskerprops=None, manage_ticks=True, autorange=False, zorder=None,\r\n",
    "capwidths=None, *, data=None)>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14353b40-e33e-4d5f-8833-9887acc1fe79",
   "metadata": {},
   "source": [
    "`pyplot.boxplot` 函数用于绘制箱线图，下面是各参数的解释："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5c73289-1554-4633-9b90-714a0678d923",
   "metadata": {},
   "source": [
    "`x`: 要绘制箱线图的数据，可以是一个或多个数据集的列表。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6cfcff42-b49d-42ad-a02f-b221224dee78",
   "metadata": {},
   "source": [
    "`notch`: 控制是否绘制缺口箱线图，默认为 `None`，表示不绘制。如果设置为 `True`，\n",
    "将绘制缺口箱线图。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a2e5fdb-f4b3-42ae-90c9-3a0a214b71ab",
   "metadata": {},
   "source": [
    "`sym`: 用于指定异常值的样式，默认为 `None`，表示使用默认样式。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88d87d82-7b26-432a-86a2-ed53e86f2f6f",
   "metadata": {},
   "source": [
    "`vert`: 控制箱线图的方向，默认为 `True`，表示垂直箱线图。如果设置为 `False`，\n",
    "将绘制水平箱线图。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf01521c-ede3-4970-9405-a331b428b1b6",
   "metadata": {},
   "source": [
    "`whis`: 控制箱线图的须的长度，默认为 `1.5`，通常情况下不需要修改。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44b056c0-bd1f-433e-80a8-2435caa9b1f3",
   "metadata": {},
   "source": [
    "`positions`: 控制箱线图的箱体位置，默认为 `None`，表示使用默认位置。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3305b1a-2a94-4639-9294-cccbb42440d2",
   "metadata": {},
   "source": [
    "`widths`: 控制箱线图的箱体宽度，默认为 `0.5`。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40f94726-ade5-402a-ba86-4f9b686330d4",
   "metadata": {},
   "source": [
    "`patch_artist`: 控制是否使用填充的方式绘制箱体，默认为 `None`，表示不填充。如果设置为\n",
    "`True`，将使用填充的方式绘制箱体。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a90f561-77b7-4bfd-8928-3875b7c52127",
   "metadata": {},
   "source": [
    "`bootstrap`: 控制是否使用自助法估计置信区间，默认为 `None`，表示不使用自助法。如果设置为整数值，则使用自助法估计置信区间的数量。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "762d6c02-d4b3-4971-a415-fd21a27add0e",
   "metadata": {},
   "source": [
    "`usermedians`: 自定义中位数的位置，默认为 `None`，表示使用默认位置。\n",
    "可以传入一个与数据集长度相同的列表，指定每个箱线图的中位数位置。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4fd152c7-971f-491c-8b0c-051c41f6bf8d",
   "metadata": {},
   "source": [
    "`conf_intervals`: 控制是否显示置信区间，默认为 `None`，表示不显示。\n",
    "可以传入一个与数据集长度相同的列表，指定每个箱线图的置信区间。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "518865c5-c759-4272-9f88-f67c9c882231",
   "metadata": {},
   "source": [
    "`meanline`: 控制是否绘制均值线，默认为 `False`，表示不绘制。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66a60f61-8ddc-46ac-bc5e-a11fb3f59875",
   "metadata": {},
   "source": [
    "`showmeans`: 控制是否显示均值点，默认为 `False`，表示不显示。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91c9ff68-a4e7-4c4c-a568-ddd81ec347e9",
   "metadata": {},
   "source": [
    "showcaps`: 控制是否显示箱线图的顶端和底端线段，默认为 `True`，表示显示。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87b8fca2-14e1-4006-91fd-08e72828dd21",
   "metadata": {},
   "source": [
    "`showbox`: 控制是否显示箱线图的箱体，默认为 `True`，表示显示。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ca45bdf-e615-45bd-9a44-ba942bff320a",
   "metadata": {},
   "source": [
    "`showfliers`: 控制是否显示异常值，默认为 `True`，表示显示。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af12d0f2-877b-46f9-87df-9d71bd1fadd6",
   "metadata": {},
   "source": [
    "`boxprops`: 控制箱体的属性，可以传入一个字典，指定箱体的颜色、线宽等属性。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c1d3284-da0c-46c1-98dc-c08257cdc28e",
   "metadata": {},
   "source": [
    "`labels`: 用于指定每个箱线图的标签，默认为 `None`，表示不显示标签。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c2e1e69-37f0-4ed7-9302-c92a77d1875a",
   "metadata": {},
   "source": [
    "`flierprops`: 控制异常值的属性，可以传入一个字典，指定异常值的颜色、标记样式等属性。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "532bd612-9ac9-4f4a-94dc-4c793f6fad73",
   "metadata": {},
   "source": [
    "`medianprops`: 控制中位数的属性，可以传入一个字典，指定中位数的颜色、线宽等属性。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98429146-c28a-4760-a61c-23e6307d2373",
   "metadata": {},
   "source": [
    "`meanprops`: 控制均值点的属性，可以传入一个字典，指定均值点的颜色、标记样式等属性。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5f066ee-f47b-4a8c-a800-0a169c306255",
   "metadata": {},
   "source": [
    "capprops`: 控制顶端和底端线段的属性，可以传入一个字典，指定线段的颜色、线宽等属性。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37105d87-b77b-4b19-8f93-b7f6fa167f21",
   "metadata": {},
   "source": [
    "`whiskerprops`: 控制须的属性，可以传入一个字典，指定须的颜色、线宽等属性。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd157855-5f82-4e36-a59b-1b6c6b06c9c9",
   "metadata": {},
   "source": [
    "`manage_ticks`: 控制是否自动管理刻度，默认为 `True`，表示自动管理。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1cf74a64-76a4-47f7-afa4-6efc8b682f6a",
   "metadata": {},
   "source": [
    "`autorange`: 控制是否自动调整坐标轴范围，默认为 `False`，表示不自动调整。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34fd91f4-6c3b-4838-bd50-2421b91084ad",
   "metadata": {},
   "source": [
    "`zorder`: 控制绘图元素的层次顺序，数字越大，元素越在前面。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7116669-0aec-40d7-8a80-48da6cf932fe",
   "metadata": {},
   "source": [
    "`zorder`: 控制绘图元素的层次顺序，数字越大，元素越在前面。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f21f709-ed4d-4a07-9f51-dbe5b39ebe45",
   "metadata": {},
   "source": [
    "1 单数据系列箱线图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4bd4d9c6-acb2-48c7-b8a8-29b0e2960072",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotx\n",
    "data = pd.read_excel('./catering_sale.xlsx',index_col='日期')\n",
    "\n",
    "plt.rcParams['font.sans-serif'] = ['Times New Roman']  # 用来正常显示中文标签\n",
    "plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号\n",
    "with plt.style.context(matplotx.styles.pitaya_smoothie['light']):\n",
    "    # 创建一个图形窗口，设置大小和分辨率\n",
    "    plt.figure(figsize=(8,6),dpi=100)\n",
    "    p = data.boxplot(return_type='dict')  # 画箱线图，直接使用dataframe的方法\n",
    "    x = p['fliers'][0].get_xdata() # 'flies'即为异常值的标签\n",
    "    y = p['fliers'][0].get_ydata()\n",
    "    y.sort() # 从小到大排序，该方法之间改变原对象\n",
    "    for i in range(len(x)):\n",
    "        if i>0:\n",
    "            plt.annotate(y[i],xy = (x[i],y[i]),xytext=(x[i]+0.05-0.8/(y[i]-y[i-1]),y[i]))\n",
    "        else:\n",
    "            plt.annotate(y[i],xy=(x[i],y[i]),xytext=(x[i]+0.08,y[i]))\n",
    "    plt.xticks(fontproperties='STsong')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b57adfc-2d90-4515-a992-997591a18c51",
   "metadata": {},
   "source": [
    "2 进阶饼图-多系列箱线图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4b5a0a8-5694-4ab7-9c4e-883c1a31657d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotx\n",
    "data = pd.read_csv('./box_data.csv')\n",
    "with plt.style.context(matplotx.styles.pitaya_smoothie['light']):\n",
    "    # 创建一个图形窗口，设置大小和分辨率\n",
    "    plt.figure(figsize=(8,6),dpi=100)\n",
    "    bplot = plt.boxplot(\n",
    "        data,\n",
    "        notch=True, # 带缺口形状\n",
    "        vert=True,  # 垂直箱线图\n",
    "        patch_artist=True,  # 填充颜色\n",
    "        labels=data.columns, # 用于标记x轴刻度\n",
    "    )\n",
    "    colors = ['pink','lightblue','lightgreen']\n",
    "    for patch,color in zip(bplot['boxes'],colors):\n",
    "        patch.set_facecolor(color)\n",
    "\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
